<?php
class StoreController extends AppController {

        var $layout = "store";
        var $name = 'Store';
        var $uses = array('Product','Cart','Order');
        var $helpers = array('Html', 'Form','Number','Common');

        function beforeFilter(){
        	if( $this->action  != 'index'){
        		$this->cart = $this->find_cart();
        	}
        	//debug($this->cart);
        }
        function index(){
                 $this->pageTitle = 'Pragprog Books オンラインストア';
                 $this->set('products',$this->Product->find('all',
                                                array('conditions' => array('Product.date_available <= ' => date('Y-n-j')))
                                        )
                );
        }
        function add_to_cart($id = null){
                $product = $this->Product->read(null,$id);
                if( $product == null ){
                        // ログ
                        $this->log("無効な商品　$id にアクセスしようとしました");
                        $this->Session->setFlash('無効な商品です');
                        $this->redirect('/store/');
                }else{  
                        $this->cart->add_product($product['Product']);
                        //debug($cart);
                        $this->Session->write('cart',$this->cart);
                        $this->redirect('/store/display_cart');
                }
        }
        
		function display_cart(){
                $this->pageTitle = '現在のカート内容';
                if( count($this->cart->items()) == 0 ){
                        $this->Session->setFlash('現在カートには商品が入っていません');
                        $this->redirect('/store/');
                }else{  
                        $this->set('items', $this->cart->items());
                        $this->set('cart', $this->cart);
                }
        }
 		function empty_cart(){
                $this->cart->do_empty();
                $this->Session->write('cart',$this->cart);;
                $this->redirect_to_index('カートが空になりました');
        }
		function checkout(){
                $this->pageTitle = 'チェックアウト';
                if( count($this->cart->items()) == 0 ){
                        $this->redirect_to_index('カートには商品が入っていません');
                }else{  
                        $this->set('payment_types',Order::$PAYMENT_TYPES);
                        $this->set('items', $this->cart->items());
                        $this->set('cart', $this->cart);
                }
        }
		function save_order(){
                $error = true;
                if( !empty($this->data)){
                        $data = $this->data;
                        $data['LineItem'] = $this->cart->items();
                        $order = $this->Order->saveAll($data);
                        if( !empty($order)){
                                $error = false;
                                $this->cart->do_empty();
                                $this->redirect_to_index('ご注文ありがとうございました');
                        }else{ 
                                $this->Session->setFlash('チェックアウトに失敗しました');
                        }
                }

                if($error){
                	    $this->set('cart',$this->cart);
                		$this->set('items', $this->cart->items());
                        $this->set('payment_types',Order::$PAYMENT_TYPES);
                        $this->render('/store/checkout');
                }

        }
         private function redirect_to_index($msg = null){
                if($msg != null){
                        $this->Session->setFlash($msg);
                }
                $this->redirect('/store/');
        }
        
        private function find_cart(){
        	$cart = $this->Session->read('cart');
        	if( $cart == null){
        		$cart = new Cart;
        	}
        	$this->Session->write('cart',$cart);
        	return $cart;
        }
}
?>
